package com.apes.framework.util;

import com.apes.framework.plugin.cache.MemoryCache;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能：锁
 *
 * @author xul
 * @create 2017-12-11 17:41
 */
@Component("lockUtil")
public class LockUtil {

    public Lock getLocalLock(String key) {
        Lock lock = (Lock) MemoryCache.getInstance().get("Lock_" + key);
        if (lock == null) {
            lock = new ReentrantLock();
            MemoryCache.getInstance().put("Lock_" + key, lock);
        }
        return lock;
    }

    public Lock getDistributedLock(String key) {
        key = "Lock_" + key;
        return getLocalLock(key); //分布式未实现
    }

}
